package cn.com.okgotest.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.okgo.OkGo;
import cn.com.okgo.callback.HttpStringCallback;
import cn.com.okgo.cookie.ICookieStore;
import cn.com.okgotest.R;
import cn.com.okgotest.fragment.base.BaseFragment;
import okhttp3.Cookie;
import okhttp3.HttpUrl;


public class CookieFragment extends BaseFragment {

    private String url="http://sjzqali.csc108.com:9800/api/news20/list?req_funType=L295&req_count=5";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_cookie, container, false);
        init(view);
        return view;
    }

    private void init(View view){

        final TextView textView=view.findViewById(R.id.textView);

        Button button1=view.findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Gson gson=new Gson();
                Map<String,String> map=new HashMap<>();
                map.put("user_id","1");
                map.put("user_lvl","2");
                map.put("phone_num","18501340201");
                map.put("access_token","1111111");
                String s=gson.toJson(map);

                HttpUrl httpUrl = HttpUrl.parse(url);
                Cookie.Builder builder = new Cookie.Builder();
                Cookie cookie = builder.name("cookie").value(s).domain(httpUrl.host()).build();
                ICookieStore ICookieStore = OkGo.getInstance().getCookieJar().getCookieStore();
                ICookieStore.saveCookie(httpUrl, cookie);

                OkGo.get(url)
                        .execute(new HttpStringCallback() {
                            @Override
                            public void onSuccess(String obj, String baseUrl) {

                            }

                            @Override
                            public void onFailed(Exception e, String baseUrl) {

                            }
                        });
            }
        });

        Button button2=view.findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HttpUrl httpUrl = HttpUrl.parse(url);
                ICookieStore cookieStore = OkGo.getInstance().getCookieJar().getCookieStore();
                List<Cookie> cookies = cookieStore.getCookie(httpUrl);
                textView.setText(cookies.toString());
            }
        });

        Button button3=view.findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ICookieStore cookieStore = OkGo.getInstance().getCookieJar().getCookieStore();
                cookieStore.removeAllCookie();
            }
        });
    }
}
